package com.mlethe.library.utils.app;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.DrawableRes;
import androidx.annotation.RequiresApi;

/**
 * 通知管理工具类
 * @author Mlethe
 * @date 2018/1/11
 */
public class NotificationUtil {

    /**
     * 创建服务启动通知
     * @param service
     * @param contentTitle
     * @param contentText
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void createNotificationChannel(Service service, String contentTitle, String contentText, @DrawableRes int smallIcon, @DrawableRes int largeIcon) {
        NotificationManager notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        // 为该通知设置一个id
        int NOTIFICATION_ID = 1;
        // 通知渠道的id
        String CHANNEL_ID = service.getPackageName() + ".channel";
        // 设定的通知渠道名称
        String channelName = service.getPackageName();
        // 设置通知的重要程度
        int importance = NotificationManager.IMPORTANCE_LOW;
        String description = "description";
        // 构建通知渠道
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
        // 配置通知渠道的属性
        channel.setDescription(description);
        // 设置通知出现时的闪灯（如果 android 设备支持的话）
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        // 设置通知出现时的震动（如果 android 设备支持的话）
        channel.enableVibration(true);
//        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        // 最后在notificationmanager中创建该通知渠道
        notificationManager.createNotificationChannel(channel);
        // Create a notification and set the notification channel.
        Notification notification = new Notification.Builder(service)
                .setContentTitle(contentTitle)  // 设置通知标题
                .setContentText(contentText)    // 设置通知内容
                .setSmallIcon(smallIcon)   // 设置通知图标
                .setLargeIcon(BitmapFactory.decodeResource(service.getResources(), largeIcon))
//                .setAutoCancel(true)    // 用户触摸时，自动关闭
                .setOngoing(true)   // 设置处于运行状态
                .setChannelId(CHANNEL_ID)
                .build();
        // 将服务置于启动状态 NOTIFICATION_ID指的是创建的通知的ID
        service.startForeground(NOTIFICATION_ID, notification);
    }

    /**
     * 创建普通通知
     * @param context
     * @param contentTitle
     * @param contentText
     */
    public static void createNotification(Context context, String contentTitle, String contentText, @DrawableRes int smallIcon, @DrawableRes int largeIcon) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // 为该通知设置一个id
        int NOTIFICATION_ID = 2;
        Notification notification = new Notification.Builder(context)
                .setContentTitle(contentTitle)  // 设置通知标题
                .setContentText(contentText)    // 设置通知内容
                .setSmallIcon(smallIcon)   // 设置通知图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), largeIcon))
                .setAutoCancel(true)    // 用户触摸时，自动关闭
                .build();
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}
